package org.mspring.mlog.service.bbs.impl;

import java.util.List;

import org.mspring.mlog.entity.bbs.Forum;
import org.mspring.mlog.service.bbs.ForumService;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013年8月6日
 */
@Service
@Transactional
public class ForumServiceImpl extends GenericDao<Forum, Long> implements ForumService {

    @Override
    public boolean hasTopic(Long forumId) {
        // TODO Auto-generated method stub
        Long count = Long.valueOf(getHibernateTemplate().uniqueResult("select count(*) from Topic topic where topic.forum.id = ?", forumId).toString());
        return count > 0;
    }

    @Override
    public List<Forum> getForumByCatalog(Long forumCatalog) {
        // TODO Auto-generated method stub
        return list("select forum from Forum forum where forum.catalog.id = ?", forumCatalog);
    }

    @Override
    public void updateForumTopics() {
        // TODO Auto-generated method stub
        executeUpdate("update Forum forum set forum.topics = (select count(*) from Topic topic where topic.forum.id = forum.id)");
    }

    @Override
    public void updateForumReplys() {
        // TODO Auto-generated method stub
        executeUpdate("update Forum forum set forum.replys = (select count(*) from TopicReply reply, Topic topic where reply.topic.id = topic.id and topic.forum.id = forum.id)");
    }
}
